package com.vrv.controller.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.AdminInfo;
import com.vrv.entity.DbInfo;
import com.vrv.entity.Form;
import com.vrv.entity.FormObj;
import com.vrv.entity.UserInfo;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/collect")
public class CollectController {

    /**
     * Description: <br>
     * 采集首页
     * 
     * @return <br>
     */
    @RequestMapping("/index")
    public String list() {
        return "/collect/collect_index";
    }

    /**
     * Description: <br>
     * 采集配置
     * 
     * @return <br>
     */
    @RequestMapping("/setting")
    public String setting() {
        return "/collect/collect_setting";
    }

    @RequestMapping("/getBeatList")
    public String getBeatList() {
        return "/collect/beat_list";
    }

    @RequestMapping("/filebeat")
    public String filebeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/filebeat";
    }

    @RequestMapping("/topbeat")
    public String topbeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/topbeat";
    }

    @RequestMapping("/tcpbeat")
    public String tcpbeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/tcpbeat";
    }

    @RequestMapping("/ptdbbeat")
    public String ptdbbeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/ptdbbeat";
    }

    @RequestMapping("/ptsyslogbeat")
    public String ptSyslogBeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/ptSyslogBeat";
    }

    @RequestMapping("/flowbeat")
    public String flowBeat(HttpServletRequest request, String ip) {
        request.setAttribute("ip", ip);
        return "/collect/flowbeat";
    }

    @RequestMapping(value = "getCollectList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getCollectList(HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "getallnodes");
        params.put("services", "all");
        if (session.getAttribute("type") != null) {
            if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                params.put("user", userInfo.getAccount());
            } else {
                UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                params.put("user", userInfo.getAccount());
            }
        }
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "getCollectInfo", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getCollectInfo(HttpServletResponse response, String nodeIp) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add(nodeIp);
        params.put("target", nodeIp);
        params.put("option", "getnodeinfo");
        params.put("nodes", list);
        params.put("object", "info");
        // params.put("object", "parameters");

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addNewNode", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addNewNode(HttpServletResponse response, String nodeIp, Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", form.getIp());
        params.put("option", "addnode");
        params.put("services", "all");
        params.put("object", "all");

        Map<String, Object> paramObject1 = new HashMap<String, Object>();

        DbInfo dbInfo = XMLRead.xmlDbInfo();
        paramObject1.put("type", dbInfo.getType());
        paramObject1.put("ip", dbInfo.getIp());
        paramObject1.put("port", dbInfo.getPort());
        paramObject1.put("dbname", dbInfo.getDbname());
        paramObject1.put("user", dbInfo.getUser());
        paramObject1.put("passwd", dbInfo.getPassword());
        Map<String, Object> paramObject2 = new HashMap<String, Object>();
        paramObject2.put("dbinfo", paramObject1);

        params.put("addnode", paramObject2);

        paramObject2.put("desc", form.getDesc());
        paramObject2.put("os", form.getOs());

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "deleteNode", produces = "application/json;charset=UTF-8")
    public @ResponseBody String deleteNode(HttpServletResponse response, String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "delnode");
        params.put("beats", "all");
        params.put("object", "all");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "getAllNodes", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getAllNodes(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "getallnodes");
        params.put("beats", "all");
        params.put("object", "all");
        if (session.getAttribute("type") != null) {
            if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                params.put("user", userInfo.getAccount());
            } else {
                UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                params.put("user", userInfo.getAccount());
            }
        }
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "getAllTableName", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getAllTableName(HttpServletResponse response, FormObj form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", form.getTarget());
        params.put("option", "datebaseinfo");
        params.put("object", "gettables");
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        paramsObj.put("type", form.getType());
        paramsObj.put("ip", form.getIp());
        paramsObj.put("port", Integer.parseInt(form.getPort()));
        paramsObj.put("dbname", form.getDbName());
        paramsObj.put("user", form.getUser());
        paramsObj.put("passwd", form.getPassword());
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("dbinfo", paramsObj);
        params.put("datebaseinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getTableCellByName", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getTableCellByName(HttpServletResponse response, String tableName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "datebaseinfo");
        params.put("object", "getfields");

        DbInfo dbInfo = XMLRead.xmlDbInfo();
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        paramsObj.put("type", dbInfo.getType());
        paramsObj.put("ip", dbInfo.getIp());
        paramsObj.put("port", Integer.parseInt(dbInfo.getPort()));
        paramsObj.put("dbname", dbInfo.getDbname());
        paramsObj.put("user", dbInfo.getUser());
        paramsObj.put("passwd", dbInfo.getPassword());
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("dbinfo", paramsObj);
        paramsObj2.put("table", tableName);
        params.put("datebaseinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getBeatInfo", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getBeatInfo(HttpServletResponse response, String ip, String beatName) {
        List<String> all = new ArrayList<String>();
        all.add(beatName);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "get");
        params.put("beats", all);
        params.put("object", "parameters");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addFileBeat", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addFileBeat(HttpServletResponse response, Form form) {
        List<String> all = new ArrayList<String>();
        all.add("filebeat");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", form.getIp());
        params.put("option", "add");
        params.put("beats", all);
        params.put("object", "parameters");

        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("path", form.getPath());
        mapObj.put("name", form.getName());
        mapObj.put("analyze", form.getAnalyze());
        mapObj.put("desc", form.getDesc());
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(mapObj);
        Map<String, Object> mapObj2 = new HashMap<String, Object>();
        mapObj2.put("list", mapList);
        Map<String, Object> mapObj3 = new HashMap<String, Object>();
        mapObj3.put("parameter", mapObj2);
        Map<String, Object> mapOb4 = new HashMap<String, Object>();
        mapOb4.put("filebeat", mapObj3);
        params.put("add", mapOb4);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addPtSyslogBeat", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addPtSyslogBeat(HttpServletResponse response, String ip,
            String targetIp) {
        List<String> all = new ArrayList<String>();
        all.add("ptsyslogbeat");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", targetIp);
        params.put("option", "set");
        params.put("beats", all);
        params.put("object", "parameters");

        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("LOGSTASH_SYSLOGHOST_IP", ip);
        Map<String, Object> mapObj2 = new HashMap<String, Object>();
        mapObj2.put("values", mapObj);
        Map<String, Object> mapObj3 = new HashMap<String, Object>();
        mapObj3.put("parameter", mapObj2);
        Map<String, Object> mapOb4 = new HashMap<String, Object>();
        mapOb4.put("ptsyslogbeat", mapObj3);
        params.put("set", mapOb4);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addFlowBeat", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addFlowBeat(HttpServletResponse response, FormObj formObj) {
        List<String> all = new ArrayList<String>();
        all.add("flowbeat");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", formObj.getTargetIp());
        params.put("option", "set");
        params.put("beats", all);
        params.put("object", "parameters");

        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("FLOWBEAT_IP", formObj.getIp());
        mapObj.put("FLOWBEAT_PORT", formObj.getPort());
        mapObj.put("DESC", formObj.getDesc());

        Map<String, Object> mapObj2 = new HashMap<String, Object>();
        mapObj2.put("values", mapObj);
        Map<String, Object> mapObj3 = new HashMap<String, Object>();
        mapObj3.put("parameter", mapObj2);
        Map<String, Object> mapOb4 = new HashMap<String, Object>();
        mapOb4.put("flowbeat", mapObj3);
        params.put("set", mapOb4);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addPtdBeat", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addPtdBeat(HttpServletResponse response, FormObj formObj) {
        List<String> all = new ArrayList<String>();
        all.add("ptdbbeat");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", formObj.getTarget());
        params.put("option", "add");
        params.put("beats", all);
        params.put("object", "parameters");

        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("type", formObj.getType());
        mapObj.put("host", formObj.getIp());
        mapObj.put("port", formObj.getPort());
        mapObj.put("name", formObj.getDbName());
        mapObj.put("user", formObj.getUser());
        mapObj.put("passwd", formObj.getPassword());
        mapObj.put("table", formObj.getAddTableName());
        mapObj.put("field", formObj.getTableCell());
        mapObj.put("isdate", true);
        mapObj.put("sql", formObj.getSql());
        mapObj.put("desc", formObj.getDesc());

        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(mapObj);
        Map<String, Object> mapObj5 = new HashMap<String, Object>();
        mapObj5.put("list", mapList);
        Map<String, Object> mapObj6 = new HashMap<String, Object>();
        mapObj6.put("parameter", mapObj5);
        Map<String, Object> mapObj7 = new HashMap<String, Object>();
        mapObj7.put("ptdbbeat", mapObj6);

        params.put("add", mapObj7);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "addTopBeat", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addTopBeat(HttpServletResponse response, boolean cpu,
            boolean process, boolean file, boolean system, String ip) {
        List<String> all = new ArrayList<String>();
        all.add("topbeat");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "set");
        params.put("beats", all);
        params.put("object", "parameters");

        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("name", "SYSTEM");
        mapObj.put("enable", system);
        Map<String, Object> mapOb5 = new HashMap<String, Object>();
        mapOb5.put("name", "CPU_PER_CORE");
        mapOb5.put("enable", cpu);
        Map<String, Object> mapOb6 = new HashMap<String, Object>();
        mapOb6.put("name", "PROCESS");
        mapOb6.put("enable", process);
        Map<String, Object> mapOb7 = new HashMap<String, Object>();
        mapOb7.put("name", "FILESYSTEM");
        mapOb7.put("enable", file);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(mapObj);
        mapList.add(mapOb5);
        mapList.add(mapOb6);
        mapList.add(mapOb7);
        Map<String, Object> mapObj2 = new HashMap<String, Object>();
        mapObj2.put("selects", mapList);
        Map<String, Object> mapObj3 = new HashMap<String, Object>();
        mapObj3.put("parameter", mapObj2);
        Map<String, Object> mapOb4 = new HashMap<String, Object>();
        mapOb4.put("topbeat", mapObj3);
        params.put("set", mapOb4);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "getBeatStatus", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getBeatStatus(HttpServletResponse response, String beatName,
            String ip) {
        List<String> all = new ArrayList<String>();
        all.add(beatName);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "get");
        params.put("beats", all);
        params.put("object", "status");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "updateState", produces = "application/json;charset=UTF-8")
    public @ResponseBody String updateState(HttpServletResponse response, String serviceName,
            Integer state, String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "set");
        params.put("beats", serviceList);
        params.put("object", "state");
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("state", state);
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        serviceMap.put(serviceName, stateMap);
        params.put("set", serviceMap);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "deleteFileBeatInfo", produces = "application/json;charset=UTF-8")
    public @ResponseBody String deleteFileBeatInfo(HttpServletResponse response, Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add("filebeat");
        params.put("target", form.getIp());
        params.put("option", "del");
        params.put("beats", serviceList);
        params.put("object", "parameters");
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("path", form.getPath());
        stateMap.put("desc", form.getDesc());
        stateMap.put("name", form.getName());
        stateMap.put("analyze", form.getAnalyze());
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        listMap.add(stateMap);
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        serviceMap.put("list", listMap);
        Map<String, Object> serviceMap2 = new HashMap<String, Object>();
        serviceMap2.put("parameter", serviceMap);
        Map<String, Object> serviceMap3 = new HashMap<String, Object>();
        serviceMap3.put("filebeat", serviceMap2);
        params.put("del", serviceMap3);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "deletePtbBeatInfo", produces = "application/json;charset=UTF-8")
    public @ResponseBody String deletePtbBeatInfo(HttpServletResponse response, FormObj form) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add("ptdbbeat");
        params.put("target", form.getIp());
        params.put("option", "del");
        params.put("beats", serviceList);
        params.put("object", "parameters");
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("type", form.getType());
        stateMap.put("host", form.getHost());
        stateMap.put("port", form.getPort());
        stateMap.put("name", form.getName());
        stateMap.put("user", form.getUser());
        stateMap.put("passwd", form.getPassword());
        stateMap.put("table", form.getTable());
        stateMap.put("field", form.getField());
        stateMap.put("sql", form.getSql());
        stateMap.put("isdate", true);
        stateMap.put("desc", form.getDesc());
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        listMap.add(stateMap);
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        serviceMap.put("list", listMap);
        Map<String, Object> serviceMap2 = new HashMap<String, Object>();
        serviceMap2.put("parameter", serviceMap);
        Map<String, Object> serviceMap3 = new HashMap<String, Object>();
        serviceMap3.put("ptdbbeat", serviceMap2);
        params.put("del", serviceMap3);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "startBeatService", produces = "application/json;charset=UTF-8")
    public @ResponseBody String startBeatService(HttpServletResponse response, String beatName,
            String ip) {
        List<String> all = new ArrayList<String>();
        all.add(beatName);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "start");
        params.put("services", all);
        params.put("object", "status");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

    @RequestMapping(value = "stopBeatService", produces = "application/json;charset=UTF-8")
    public @ResponseBody String stopBeatService(HttpServletResponse response, String beatName,
            String ip) {
        List<String> all = new ArrayList<String>();
        all.add(beatName);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "stop");
        params.put("services", all);
        params.put("object", "status");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localbeat", json);
        return str;
    }

}
